package com.example.phonestore.service.impl;

import com.example.phonestore.controller.UserController;
import com.example.phonestore.entity.Address;
import com.example.phonestore.mapper.AddressMapper;
import com.example.phonestore.service.AddressService;
import com.example.phonestore.service.CityService;
import com.example.phonestore.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.phonestore.util.JsonResult.success;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private CityService cityService;
    @Override
    public JsonResult insertAddress(Integer uid,Address address) {
        JsonResult jsonResult=new JsonResult();
        String provinceName=cityService.getNameByCode(address.getProvinceCode());
        String cityName=cityService.getNameByCode(address.getCityCode());
        String areaName=cityService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        address.setUid(uid);
        System.out.println(address);
        Integer r=addressMapper.insertAddress(address);
        if (r==1){
            return success();
        }
        jsonResult.setMsg("添加新地址失败！");
        return jsonResult;
    }

    @Override
    public List<Address> getAddressByUid(Integer uid) {
        List<Address> addressList=addressMapper.findByUid(uid);
//        for (Address address:addressList){
//            address.setAid(null);
//            address.setUid(null);
//            address.setProvinceCode(null);
//            address.setCityCode(null);
//            address.setAreaCode(null);
//        }
        System.out.println(addressList);
        return addressList;
    }

    @Override
    public JsonResult deleteOneAddress(Integer aid) {
        JsonResult jsonResult=new JsonResult();
        Integer r=addressMapper.deleteByAid(aid);
        if (r==1){
            return success();
        }
        jsonResult.setMsg("删除收货地址失败！");
        return jsonResult;
    }
}
